/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.cip.esymons.tramite.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import org.cip.esymons.tramite.beans.viaController;
import org.cip.esymons.general.be.Via;

/**
 *
 * @author ADMINISTRADOR
 */
public class viaConverter implements Converter{

    public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) throws ConverterException {

        if (arg2 == null) {
            return null;
        }
        Integer codigo=null;
        try {
            codigo = new Integer(arg2);
        } catch (Exception ex) {
            return null;
        }
        viaController controller = (viaController) FacesContext.getCurrentInstance().getApplication().getVariableResolver().resolveVariable(FacesContext.getCurrentInstance(), "viaController");
        return controller.buscarPorCodigo(codigo);
    }

    public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) throws ConverterException {

        if (arg2 == null) {
            return null;
        }
        if (arg2 instanceof Via) {
            if (((Via) arg2).getViaCodigo() == null) {
                return null;
            } else {
                return ((Via) arg2).getViaCodigo().toString();
            }
        } else {
            return null;
        }
    }

}
